package bs.app.Main;

import android.graphics.Canvas;
import bs.app.Client.AppPage1;
import bs.app.Client.AppPage2;
import bs.app.Client.AppPage5;
import bs.app.Client.CardPage;
import bs.app.Client.DialogBox;
import bs.app.Client.PageManager;
import bs.app.Client.SearchPage;
import bs.app.Client.SplashScreen;
import bs.app.Client.SubMenuItem;
import bs.app.Client.isChangeLoadingState;
import bs.app.HTTPConnection.CConnection;
import bs.app.Util.BitMapFont;
import bs.app.android.CanvasJ2ME;
import bs.app.android.Graphics;
import bs.app.android.Image;

public class CCanvas extends CanvasJ2ME /*implements Runnable */{
	public boolean isbrun = false; // Flag Run Game
	public static boolean isMoto; // moto machine
	public static boolean[] keyPressed = new boolean[21]; // Press Key Type
	public static boolean[] keyHold = new boolean[21]; // Hold Key Type
	public static int s_iCurrentAsciiKeyPressed[] = new int[1]; // AsciiKeyPress of Qwerty
	public static int currentkeycode = 0;
	public static boolean isPointerDown, isPointerPressed, isPointerReleased; // Touch  or Not Touch Screen
	public static int s_iPx, s_iPy; // Point Touch
	public static CCanvas s_Instan; // Instan Canvas
	public static int CurrentScreen = 0; // State Game
	public static int s_iWcanvas, s_iHcanvas; // Width And Height
	public static final int SplashScreen = 1;
	public static final int AppPage1 = 2;
	public static final int AppPage2_3_4_6 = 3;
	public static final int AppPage5 = 4;
	public static final int isChangeLoadingState = 5;
	public static final int isSubMenu = 6;
	public static final int isDialog = 7;
	public static final int IsSearchPage = 8;
	public static final int IsCardPage = 9;
	public static int SaveStatePrevious = 0;
	
	// tao instans
	public CConnection connect;
	public static SplashScreen splash;
	public static AppPage1 apppage1;
	public static AppPage2 apppage2;
	public static AppPage5 apppage5;
	public static BitMapFont mediumfont;
	public static BitMapFont smallfont;
	public static DialogBox dialog;
	public static isChangeLoadingState LoadingState;
	public static SubMenuItem submenu;
	public static SearchPage searchpage;
	public static CardPage cardPage;
	
	// //////////////////////
	public static Image ImgBuffer;
	public static Graphics gBuffer;
	public static int langue = 0;

	//public static String platform = "";
	public static boolean ispaintBandwidth = false;

	public static int xTranslate, yTranslate;

	// Test phone nokia
	public boolean isNokia = false;

	public static String sError="";
	public CCanvas() {
		// Set Full Screen
		setFullScreenMode(true);
		mediumfont = new BitMapFont("MyriadPro_Regular.otf" , 0);
		smallfont = new BitMapFont("MyriadPro_Regular.otf", 1);
//		platform = System.getProperty("microedition.platform");
//		// isnokia		
//		try {
//			//System.out.println("Inside Canvas Test");
//			Class.forName("com.nokia.mid.ui.FullCanvas");
//			isNokia = true;
//		} catch (Exception e) {
//			isNokia = false;
//		}
//		if(platform.indexOf("U1280")!= -1)
//			isNokia = true;
		// Instan Canvas
		s_Instan = this;
		// Get Value Width And Height Canvas
		s_iWcanvas = this.getWidth();
		s_iHcanvas = this.getHeight();		
		xTranslate = 0;
		yTranslate = 0;

		// ImgBuffer =Image.createImage(s_iWcanvas,s_iHcanvas);
		ImgBuffer = Image.createImage(s_iWcanvas, s_iHcanvas);
		gBuffer = ImgBuffer.getGraphics();
		// Moto Aciton key
		isMoto = (getKeyCode(FIRE) == -20);

		splash = new SplashScreen();
		CurrentScreen = SplashScreen;

		connect = new CConnection();
		connect.setListenner(PageManager.getInstance());

		try {
//			connect.getViaHttpsConnection("http://einfo.giaitri24.vn/?ctrl=a");
		} catch (Exception e) {
			e.printStackTrace();
		}
		// ///////////////// create Instan Dialog and Loading
		dialog = new DialogBox();
		LoadingState = new isChangeLoadingState();
		submenu = new SubMenuItem();
		//Thread canvasthread = new Thread(this);
		//canvasthread.start();
	}

	// key press
	public void keyPressed(int keyCode) {
		currentkeycode = keyCode;
		if (isMoto) // moto key
		{
			switch (keyCode) {
			case -21:
			case 21:
				keyCode = -6;
				break;
			case -22:
			case 22:
				keyCode = -7;
				break;
			case -23:
				keyCode = -8;
				break;
			case -2:
				;
				keyCode = -3;
				break;
			case -5:
				keyCode = -4;
				break;
			case -6:
				keyCode = -2;
				break;
			case -20:
				keyCode = -5;
				break;
			}
		}

		if (keyCode >= 32) // Qwerty Key
			s_iCurrentAsciiKeyPressed[0] = keyCode;

		switch (keyCode) // Normal Key
		{
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			keyPressed[keyCode - 48] = true;
			keyHold[keyCode - 48] = true;
			return; // 0 -> 9
		case 42:
			keyHold[10] = true;
			keyPressed[10] = true;
			return; // Key [*]
		case 35:
			keyHold[11] = true;
			keyPressed[11] = true;
			return; // Key [#]
		case -1:
		case -38:
			keyHold[12] = true;
			keyPressed[12] = true;
			return; // UP
		case -2:
		case -39:
			keyHold[13] = true;
			keyPressed[13] = true;
			return; // DOWN
		case -3:
			keyHold[14] = true;
			keyPressed[14] = true;
			return; // LEFT
		case -4:
			keyHold[15] = true;
			keyPressed[15] = true;
			return; // RIGHT
		case -5:
		case 10:
		case 13:
			keyHold[16] = true;
			keyPressed[16] = true;
			return; // [i]
		case -21:
		case -6:
			keyHold[17] = true;
			keyPressed[17] = true;
			return; // Soft1
		case -22:
		case -7:
			keyHold[18] = true;
			keyPressed[18] = true;
			return; // Soft2
		case -8:
		case 8:
			keyHold[19] = true;
			keyPressed[19] = true;
			return; // Clear
		case -11:
			keyHold[20] = true;
			keyPressed[20] = true;
			return; // Back (Sony)
		}
	}

	// key Released
	public void keyReleased(int keyCode) {
		if (isMoto) {
			switch (keyCode) {
			case -21:
				keyCode = -6;
				break;
			case -22:
				keyCode = -7;
				break;
			case -23:
				keyCode = -8;
				break;
			case -2:
				keyCode = -3;
				break;
			case -5:
				keyCode = -4;
				break;
			case -6:
				keyCode = -2;
				break;
			case -20:
				keyCode = -5;
				break;
			}
		}
		switch (keyCode) {
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			keyHold[keyCode - 48] = false;
			return; // 0 -> 9
		case 42:
			keyHold[10] = false;
			return; // Key [*]
		case 35:
			keyHold[11] = false;
			return; // Key [#]
		case -1:
		case -38:
			keyHold[12] = false;
			return; // UP
		case -2:
		case -39:
			keyHold[13] = false;
			return; // DOWN
		case -3:
			keyHold[14] = false;
			return; // LEFT
		case -4:
			keyHold[15] = false;
			return; // RIGHT
		case -5:
		case 10:
		case 13:
			keyHold[16] = false;
			return; // [i]
		case -6:
		case -21:
			keyHold[17] = false;
			return; // Soft1
		case -7:
		case -22:
			keyHold[18] = false;
			return; // Soft2
		case -8:
		case 8:
			keyHold[19] = false;
			return; // Back
		case -11:
			keyHold[20] = false;
			return; // Back (Sony)
		}
	}

	// reset key
	public static void resetkey() {
		for (int i = 0; i < keyPressed.length; i++)
			keyPressed[i] = false;
	}

	// reset PointerPress
	public static void resetPointerPress() {
		isPointerPressed = false;
		isPointerDown = false;
	}

	// Point Touch Dragged
	protected void pointerDragged(int x, int y) {
		isPointerDown = true;
		s_iPx = x;
		s_iPy = y;
	}

	// Touch Point
	protected void pointerPressed(int x, int y) {
		isPointerDown = true;
		isPointerPressed = true;
		s_iPx = x;
		s_iPy = y;
	}

	// Release Point
	protected void pointerReleased(int x, int y) {
		isPointerDown = false;
		isPointerReleased = true;
		s_iPx = x;
		s_iPy = y;
		if (CurrentScreen == AppPage5) {
			isPointerPressed = false;
			isPointerDown = false;
		}
	}

	// change with and height cavas
	protected void sizeChanged(int ww, int hh) {
		s_iWcanvas = ww;
		s_iHcanvas = hh;
	}

	public void Translate(Graphics g) {
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		// g.translate(-s_iTranslateX, -s_iTranslateY);
	}

	public boolean ispaint(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
		if (x2 < x1 + w1 && x1 < x2 + w2 && y2 < y1 + h1 && y1 < y2 + h2)
			return true;
		return false;
	}

	public void paint(Graphics g) {
		if (!isNokia) {
			//CMidlet.instanMidlet.display.flashBacklight(100);
			setFullScreenMode(true);
		}
		Translate(g);		
//		g.setClip(0, 0, s_iWcanvas, s_iHcanvas);
		g.setColor(0x000000);
		g.fillRect(0, 0, s_iWcanvas, s_iHcanvas);
//		g.setClip(s_iTranslateX, s_iTranslateY, s_WSizeScreen, s_HSizeScreen);

		switch (CurrentScreen) // State Screen
		{
		case SplashScreen:
			splash.Paint(gBuffer);
			g.drawImage(ImgBuffer, 0, 0, 0);
			break;
		case AppPage1:
			apppage1.Paint(gBuffer);
			g.drawImage(ImgBuffer, 0, 0, 0);
			break;
		case AppPage2_3_4_6:
			apppage2.Paint(gBuffer);
			g.drawImage(ImgBuffer, 0, 0, 0);
			break;
		case AppPage5:
			apppage5.Paint(gBuffer);
			g.drawImage(ImgBuffer, 0, 0, 0);
			break;
		case isChangeLoadingState:
			if (LoadingState.timeForLoading > 0) {
				LoadingState.painttransparent(gBuffer);
				LoadingState.timeForLoading--;
			}
			LoadingState.paint(gBuffer);
			g.drawImage(ImgBuffer, 0, 0, 0);
			break;
		case isDialog:
			dialog.Paint(gBuffer);
			g.drawImage(ImgBuffer, 0, 0, 0);
			break;
		case isSubMenu:
			g.drawImage(ImgBuffer, 0, 0, 0);
			submenu.paint(g);
			break;
		case IsSearchPage:
			if (searchpage.timeForLoading > 0) {
				searchpage.Painttransparent(gBuffer);
				searchpage.timeForLoading--;
			}
			g.drawImage(ImgBuffer, 0, 0, 0);
			searchpage.painttextfield(g);
			break;
		case IsCardPage:			
			g.drawImage(ImgBuffer, 0, 0, 0);
			cardPage.painttextfield(g);
			break;
		}
		g.setColor(255, 0, 0);
//		g.drawString(platform, 50, 50, 0);
		if (ispaintBandwidth)
			CCanvas.smallfont.drawString(g, (CCanvas.s_Instan.connect.bandwidth / 1000) + "KB", s_iWcanvas / 2, 4, 0, 0xffffff, 0xffffff, false);
		
	}

	// Run Thread Game
	public void run() {
//		while (!isbrun) {
//			long time = System.currentTimeMillis(); // Get Current Time

			switch (CurrentScreen) // State Game
			{
			case SplashScreen:
				splash.UpdateKeyPress(keyPressed, keyHold, s_iCurrentAsciiKeyPressed);
				break;
			case AppPage1:
				apppage1.pointerPressed(s_iPx, s_iPy);
				apppage1.UpdateKeyPress(keyPressed, keyHold, s_iCurrentAsciiKeyPressed);
				SaveStatePrevious = AppPage1;
				break;
			case AppPage2_3_4_6:
				apppage2.pointerPressed(s_iPx, s_iPy);
				apppage2.UpdateKeyPress(keyPressed, keyHold,s_iCurrentAsciiKeyPressed);
				SaveStatePrevious = AppPage2_3_4_6;
				break;
			case AppPage5:
				apppage5.pointerPressed(s_iPx, s_iPy);		
				apppage5.pointerDragged(s_iPx, s_iPy);
				apppage5.UpdateKeyPress(keyPressed, keyHold, s_iCurrentAsciiKeyPressed);
				SaveStatePrevious = AppPage5;
				break;
			case isChangeLoadingState:
				LoadingState.UpdateKeyPress(keyPressed, keyHold, s_iCurrentAsciiKeyPressed);
				break;
			case isDialog:
				dialog.pointerPressed(s_iPx, s_iPy);
				dialog.UpdateKeyPress(keyPressed, keyHold,s_iCurrentAsciiKeyPressed);
				break;
			case isSubMenu:
				submenu.pointerPressed(s_iPx, s_iPy);
				submenu.UpdateKeyPress(keyPressed, keyHold, s_iCurrentAsciiKeyPressed);
				break;
			case IsSearchPage:
				searchpage.pointerPressed(s_iPx, s_iPy);
				searchpage.UpdateKeyPress(keyPressed, keyHold,s_iCurrentAsciiKeyPressed);
				break;
			case IsCardPage:
				cardPage.pointerPressed(s_iPx, s_iPy);
				cardPage.UpdateKeyPress(keyPressed, keyHold,s_iCurrentAsciiKeyPressed);
				break;
			}
//			repaint(); // Repaint Canvas
//			serviceRepaints(); // Service Repaints
//			while (System.currentTimeMillis() - time < 50) // Thread Loop
//			{
//				Thread.yield();
//			}
//		}
//		CMidlet.instanMidlet.exit();
	}
}
